﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabManager.UserControls.Tabs.Templates;
using DataConnector;
using Library;
using Equin.ApplicationFramework;
using System.Data.Entity;
using LabManager.Forms;

namespace LabManager.UserControls.Tabs.Products
{
    public partial class TabProductObject : TabTemplate
    {
        private BindingListView<V_ProductObject> bindingList;
        private BindingSource bindingSource = new BindingSource();
        private int selectedID = -1;

        public TabProductObject()
        {
            InitializeComponent();
        }

        public override void Load()
        {
            SetTexts();
            LoadProductObjectList();
            LoadStates();
        }

        #region Events

        private void dgMain_SelectionChanged(object sender, EventArgs e)
        {
            if (dgMain.SelectedRows.Count > 0)
            {
                var selectedRow = dgMain.SelectedRows[0];

                selectedID = (int)selectedRow.Cells[0].Value;
                tbBarcode.Text = selectedRow.Cells[1].Value.ToString();
                tbProductType.Text = selectedRow.Cells[2].Value.ToString();
                tbLOT.Text = selectedRow.Cells[3].Value.ToString();
                dtTagDate.Text = selectedRow.Cells[4].Value.ToString();
                dtExpirationDate.Text = selectedRow.Cells[5].Value.ToString();
                cbExpired.Enabled = DateTime.Parse(dtExpirationDate.Text) < DateTime.Now.AddDays(-1);
            }

            var canCreateProductObjects = DataConnectorLayer.Instance.R_Product.FindOne(x => x.ProductID > 0) != null;
            btNew.Enabled = canCreateProductObjects;

            btDelete.Enabled = dgMain.RowCount > 0;
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            var objectsForm = new ObjectsForm();
            objectsForm.ShowDialog(this);

            if (objectsForm.ProductID > 0)
            {
                for (int i = 0; i < objectsForm.Amount; i++ )
                {
                    var item = new ProductObject() { ProductID = objectsForm.ProductID, BarcodeNumber = SerialConnectorLayer.Instance.GetNextBarcode() };
                    DataConnectorLayer.Instance.R_ProductObject.Create(item);
                    DataConnectorLayer.Instance.R_ProductObject.SaveChanges();

                    // Trail
                    var stateTrail = new ProductStateTrail() { ProductObjectID = item.ProductObjectID, UserID = 1, StateID = 1 };
                    DataConnectorLayer.Instance.R_ProductStateTrail.Create(stateTrail);
                    DataConnectorLayer.Instance.R_ProductStateTrail.SaveChanges();
                }

                LoadProductObjectList();
            }
        } 

        private void btDelete_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_ProductObject.FindById(selectedID);
            DataConnectorLayer.Instance.R_ProductObject.Delete(item);
            DataConnectorLayer.Instance.R_ProductObject.SaveChanges();

            LoadProductObjectList();
        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {
            var search = tbFilter.Text.ToLower();

            bindingList.ApplyFilter(delegate(V_ProductObject productObject)
            {
                return productObject.BarcodeNumber.ToLower().Contains(search) ||
                        productObject.Description.ToLower().Contains(search) ||
                        productObject.ExpirationDate.ToString().ToLower().Contains(search) ||
                        productObject.LOT.ToLower().Contains(search) ||
                        productObject.OrderNumber.ToLower().Contains(search) ||
                        productObject.ProductDescription.ToLower().Contains(search) ||
                        productObject.TagDate.ToString().ToLower().Contains(search);
            });
        }

        #endregion

        #region Helpers

        private void SetTexts()
        {
            //TODO
        }

        private void LoadStates()
        {
            var states = new BindingListView<State>(DataConnectorLayer.Instance.R_State.FindAll(x => x.StateID > 0).ToList<State>());

            cbState.DataSource = states;
            cbState.ValueMember = "StateID";
            cbState.DisplayMember = "StateName";  
        }

        private void LoadProductObjectList()
        {
            bindingList = new BindingListView<V_ProductObject>(DataConnectorLayer.Instance.R_V_ProductObject.FindAll(x => x.ProductObjectID > 0).ToList<V_ProductObject>());
            bindingSource.DataSource = bindingList;
            dgMain.DataSource = bindingSource;
            bnNavi.BindingSource = bindingSource;

            if (dgMain.Rows.Count > 0)
            {
                selectedID = (int)dgMain.Rows[0].Cells[0].Value;
            }

            dgMain_SelectionChanged(null, null);
        }

        #endregion

    }
}